package com.qzsoft.zhjd.asphalt.modules.daystatistics.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.qzsoft.zhjd.asphalt.modules.asphaltdatasieve.dto.AsphaltDataSievingAvgDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltdatasieve.service.AsphaltDataSieveService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchThresholdEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchTypeEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchHotService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchThresholdService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchTypeService;
import com.qzsoft.zhjd.asphalt.modules.daystatistics.dto.AsphaltDayStatisticsDetailsDto;
import com.qzsoft.zhjd.asphalt.modules.daystatistics.dto.AsphaltDayStatisticsDto;
import com.qzsoft.zhjd.asphalt.modules.daystatistics.mapper.AsphaltDayStatisticsMapper;
import com.qzsoft.zhjd.asphalt.modules.daystatistics.service.AsphaltDayStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.service.SysStationService;

/**
 * @ClassName AsphaltDayStatisticsServiceImpl
 * @Description 全天生产数据业务接口实现
 * @author linhuawei
 * @since 2022-01-29
 */
@Service
public class AsphaltDayStatisticsServiceImpl implements AsphaltDayStatisticsService {

    @Autowired
    private AsphaltDayStatisticsMapper asphaltDayStatisticsMapper;

    @Autowired
    private AsphaltDataSieveService asphaltDataSieveService;

    @Autowired
    private AsphaltMatchService asphaltMatchService;

    @Autowired
    private AsphaltMatchTypeService asphaltMatchTypeService;

    @Autowired
    private AsphaltMatchThresholdService asphaltMatchThresholdService;

    @Autowired
    private SysStationService sysStationService;

    @Autowired
    private AsphaltMatchHotService asphaltMatchHotService;

    @Override
    public AsphaltDayStatisticsDto getDayChartData(String stationCode, String matchCode, String day) {
        // 返回结果
        AsphaltDayStatisticsDto dto = new AsphaltDayStatisticsDto();

        // 获取配方编号
        AsphaltMatchEntity match = asphaltMatchService.getByCode(matchCode);
        if (null != match) {
            // 获取上下限
            AsphaltMatchThresholdEntity matchThreshold = asphaltMatchThresholdService
                    .getByMatchName(match.getMatchName());
            dto.setMatchThreshold(matchThreshold);

            // 获取级配类型
            AsphaltMatchTypeEntity matchType = asphaltMatchTypeService.getByType(match.getMatchType());
            dto.setMatchType(matchType);

            // 获取热料配置信息
            Map<String, BigDecimal> targetMap = asphaltMatchHotService.getTargetMap(match.getId());
            dto.setTargetMap(targetMap);

            stationCode = match.getStationCode();
        }
        dto.setMatch(match);

        // 获取拌合楼信息
        SysStationEntity station = null;
        if (ObjectUtils.isNotEmpty(stationCode)) {
            station = sysStationService.getStationByCode(stationCode);
        }
        dto.setStation(station);

        // 获取筛分数据
        AsphaltDataSievingAvgDto sieveDataAvg = asphaltDataSieveService.avgForDay(stationCode, matchCode, day);
        dto.setSieveDataAvg(sieveDataAvg);

        // 获取全部生产数据
        List<AsphaltDayStatisticsDetailsDto> details = asphaltDayStatisticsMapper.getDetails(stationCode, matchCode, day);
        dto.setDetails(details);

        return dto;
    }

}
